<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/22
 * Time: 14:12
 */
namespace app\admin\controller;
use app\admin\model\DataBases;
use app\admin\model\Models;
use think\Controller;
use think\Request;

class Model extends Controller
{
    public function show_list(){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('model/del')
            ->addTableHeaderAdd('model/show_add')
            ->addTableColumn('id','text','ID')
            ->addTableColumn('model_name','text','表名称')
            ->addTableColumn('model_table','text','表')
            ->addTableColumn('model_type','text','表类型')
            ->addTableColumn('table_engine','text','引擎')
            ->addTableColumn('status','switch','状态',['开启','禁用'],'model/change')
            ->addTableRightHeader('操作',null,null,230)
            ->addTableRight('a','fields/show_list','添加字段')
            ->addTableRight('edit','model/show_edit','修改')
            ->addTableRight('del','model/del','删除')
            ->addTablePage('model/page')
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $data = Models::where('id','>',0)
            ->paginate($limit)
            ->toArray();
        return json_page_response($data);
    }

    /**改变状态
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function change(Request $request){
        $id = $request->param('id');
        $data = Models::where('id',$id)
            ->findOrEmpty()->toArray();
        if($data['status']==1){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(Models::where('id',$data['id'])->update([
            'status'=>$data['status']
        ])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }

    /**查看添加模型
     * @param Request $request
     * @return mixed
     */
    public function show_add(Request $request){
        return NewBuilder::make('form')
            ->addAction("model/add")
            ->addInput('model_name','模型名称')
            ->addInput('model_table','表名称')
            ->addSelect('model_type','模型类型','',[
                '单表模型','联表模型'
            ])
            ->addInput('table_engine','模型引擎','innodb')
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**添加表
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        $databases = new DataBases();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if($databases->addTableAll($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**查看编辑
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data = Models::where('id',$id)->findOrEmpty()->toArray();
        return NewBuilder::make('form')
            ->addAction("model/edit")
            ->addNone('id',$data['id'])
            ->addInput('model_name','模型名称',$data['model_name'])
//            ->addSelect('model_type','模型类型',$data['model_type'],['单表模型','联表模型'])
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**查看
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function edit(Request $request){
        $data = $request->param();
//        if(isset($data['status'])&&$data['status']=='on'){
//            $data['status'] = 1;
//        }else{
//            $data['status'] = 0;
//        }
        $models = new Models();
        if($models->allowField(true)->save($data,['id'=>$data['id']])){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**删除模型
     * @param Request $request
     * @return \think\response\Json
     */
    public function del(Request $request){
        $id = $request->param('id');
        $databases = new DataBases();
        if($databases->delAll($id)){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }
}